﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using PhotoGallery.BLL;
using PhotoGallery.Model;
public partial class album : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        User u = (User)Session["user"];
        //未登录
        if (u == null)
        {
            Response.Redirect("login.aspx");
        }
        else 
        {
            //初次创建相册
            if (Request.QueryString["aid"] == null||Request.QueryString["aid"]=="")
            {
                this.cover.Visible = false;
            } 
            else//修改相册信息和上传封面图片
            {   
                //取得相册的名称和描述，并且初始化相册的Session对象
                String aid=Request.QueryString["aid"].ToString();
                this.uplink.Visible = true;
                this.rblock.Visible = false;
                this.uplink.NavigateUrl = "picupload.aspx?aid=" + aid;
                this.albumlink.NavigateUrl = "albumshow.aspx?aid=" + aid;
                
                AlbumAction aa = new AlbumAction();
                Albums album=aa.showAlbum(aid);
                Session["album"]=album;
                if (!IsPostBack) 
                { 
                    this.albumName.Value = album.Title;
                    this.albumDesp.Value = album.Desp;
                    this.cover.Visible = true;
                    this.albumlink.Text = album.Title;
                    h1.InnerText = "修改相册信息";
                    createAlbum.Text = "确认修改";
                }
                    //取出封面图片
                    PhotoAction pa = new PhotoAction();
                    if (album.Cover != "0")//已经有封面图片
                    {
                        Image1.Visible = true;
                        Image1.ImageUrl = pa.showPic(album.Cover).Pic;
                    }
                    else //还没有图片，
                    {
                        Image1.Visible = false;
                    }
                
            }
           
        }
        
    }
    /*上传图片*/
    protected void uploadButton_Click(object sender, EventArgs e)
    {
        if (this.coverUpload.PostedFile!=null) 
        {
            string photoName1 = coverUpload.PostedFile.FileName; //获取初始文件名
            int i = photoName1.LastIndexOf("."); //取得文件名中最后一个"."的索引
            string newext = photoName1.Substring(i); //获取文件扩展名
            //判断文件是否为图片
            if (newext != ".gif" && newext != ".jpg" && newext != ".jpeg" && newext != ".bmp" && newext != ".png"
                && newext!=".GIF" && newext !=".JPG" && newext != ".JPEG" && newext !=".BMP" && newext !=".PNG")
            {
                Response.Write("文件格式不正确!");
                Response.End();
            }
            string classid = ((User)Session["user"]).Uid;
            if (!Directory.Exists(HttpContext.Current.Server.MapPath("photos/") + "\\" + classid))
            {
                System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("photos/") + "\\" + classid);
            }
            //修改文件名
            photoName1 = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-") + photoName1;
            coverUpload.PostedFile.SaveAs(Server.MapPath("photos\\" + classid + "\\" + photoName1));
            string path = "photos/" + classid + "/" + photoName1;
            string coverid=picUpload(photoName1, ((Albums)Session["album"]), classid, path);
            if (coverid != "0")
            {
                Response.Redirect("album.aspx?aid=" + ((Albums)Session["album"]).Aid);
            }
            
        }
    }
    //图像存入数据库
    private string picUpload(string filename,Albums album,string uid,string path)
    {   
        //图像存入数据库
        PhotoAction pa = new PhotoAction();
        pa.uploadPictures(uid, filename, "",album.Aid, path,"1");
        Picture p=pa.getPicbyUidndFileName(uid,filename);
       
        if (p != null)
        {   
            //取得刚上传图片的id
            string id = p.Id;
            //修改封面
            AlbumAction aa = new AlbumAction();
            aa.modifyAlbum(album.Aid, id);
            return id;
        }
        else
        {
            return "0";
        }
    }
    //创建相册
    protected void createAlbum_Click(object sender, EventArgs e)
    {
        if (Session["user"] == null) 
            Response.Redirect("login.aspx");
        if (Request.QueryString["aid"] == null)
        {
            string title = this.albumName.Value;
            string desp = this.albumDesp.Value;
            AlbumAction aa = new AlbumAction();
            Albums a = aa.newAlbum(((User)Session["user"]).Uid, title, desp);
            if (a != null)
            {
                Response.Redirect("album.aspx?aid=" + a.Aid);
            }
            else
                return;
        }
        else //修改相册信息
        {
            if (Session["album"] != null) 
            {
                Albums myalbum = new Albums();
                myalbum.Title = this.albumName.Value;
                myalbum.Desp = this.albumDesp.Value;
                AlbumAction aa = new AlbumAction();
                aa.modifyAlbum(((Albums)Session["album"]).Aid, myalbum);
            }
        }
    }
    //判断图片格式是否合法
    public void isImage(object source, ServerValidateEventArgs args)
    {
        string photoName1 = coverUpload.PostedFile.FileName;
        int i = photoName1.LastIndexOf("."); //取得文件名中最后一个"."的索引
        string newext = photoName1.Substring(i);
        if (newext != ".gif" && newext != ".jpg" && newext != ".jpeg" && newext != ".bmp" && newext != ".png"
                && newext != ".GIF" && newext != ".JPG" && newext != ".JPEG" && newext != ".BMP" && newext != ".PNG")
        {
            args.IsValid = false;
        }
        else 
        {
            args.IsValid = true;
        }
    }
}